/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.promo.impl;

import dao.promo.PromoPriceDao;
import java.util.List;
import java.util.Map;
import model.promo.PromoPrice;
import model.promo.PromoPriceId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.promo.PromoPriceService;
import utility.NameValueList;
import utility.StringUtils;

@Service("promoPriceService")
@Transactional(readOnly=true)
public class PromoPriceServiceImpl implements PromoPriceService{
    
    String mainQuery = "select promoPrice from PromoPrice promoPrice "+
               "left join fetch promoPrice.promo promo left join fetch promoPrice.item item ";
    
    @Autowired private PromoPriceDao dataDao;

    @Override
    @Transactional(readOnly=false)
    public boolean save(PromoPrice data) {
        return dataDao.save(data);
    }

    @Override
    @Transactional(readOnly=false)
    public boolean delete(PromoPrice data) {
        return dataDao.delete(data);
    }

    @Override
    public PromoPrice getPromoPrice(PromoPriceId id) {
        String query = mainQuery+" where promoPrice.id.id = "+id.getId()+" and promoPrice.id.itemId = "+id.getItemId();
        return dataDao.getByCustomQuery(query);
    }

    @Override
    public Long count(Map<NameValueList, Object> params) {
        String query= "select count(*) from PromoPrice promoPrice "+StringUtils.getFilter(params);
        return dataDao.count(query);
    }

    @Override
    public List<PromoPrice> getAll(int id) {
        String query = mainQuery+" where promoPrice.id.id = "+id+" order by item.nama";
        return dataDao.getAll(query);
    }
    
}
